﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;

namespace WXYW
{
    public partial class MaintainRecordDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitialPage();
            }

        }
        private void InitialPage()
        {
            ApplicationProductService service = new ApplicationProductService();
            ApplicationProduct product = service.GetApplicationProductByID(long.Parse(Request.QueryString[Constants.QUERYSTR_PRODUCT_ID]));

            if (!product.ApplicationFormReference.IsLoaded)
            {
                product.ApplicationFormReference.Load();
            }
            if (!product.ApplicationForm.UserReference.IsLoaded)
            {
                product.ApplicationForm.UserReference.Load();
            }
            if (!product.ApplicationForm.ServiceStationReference.IsLoaded)
            {
                product.ApplicationForm.ServiceStationReference.Load();
            }
            if (!product.ProblemTypeReference.IsLoaded)
            {
                product.ProblemTypeReference.Load();
            }
            if (!product.ProductTypeReference.IsLoaded)
            {
                product.ProductTypeReference.Load();
            }
            if (!product.ApplicationForm.GatheringReference.IsLoaded)
            {
                product.ApplicationForm.GatheringReference.Load();
            }

            rmaCodeLabel.Text = product.ApplicationForm.ApplicationCode;
            runningNumberLabel.Text = product.ApplicationForm.RunningNumber;

            // user information
            clientNameLabel.Text = product.ApplicationForm.User.FullName;
            connectPersonLabel.Text = product.ApplicationForm.User.ContactPerson;
            phoneNumberLabel.Text = product.ApplicationForm.User.UserPhone;
            cellPhoneLabel.Text = product.ApplicationForm.User.UserMobilePhone;
            faxLabel.Text = product.ApplicationForm.User.UserFax;
            addressLabel.Text = product.ApplicationForm.User.UserAddress;
            emailLabel.Text = product.ApplicationForm.User.UserEmail;
            registerLabel.Text = product.ApplicationForm.User.RegisterTime.HasValue ? product.ApplicationForm.User.RegisterTime.Value.ToString("yyyy-MM-dd") : "";

            // serivice station
            if (product.ApplicationForm.ServiceStation != null)
            {
                serviceStationNameLabel.Text = product.ApplicationForm.ServiceStation.Name;
            }
            //serviceStationConnectPersonLabel.Text = product.ApplicationForm.ServiceStation.User1.FirstOrDefault().ContactPerson;
            //serviceStationPhoneLabel.Text = product.ApplicationForm.ServiceStation.User1.FirstOrDefault().UserPhone;
            //serviceStationMobileLabel.Text = product.ApplicationForm.ServiceStation.User1.FirstOrDefault().UserMobilePhone;
            //serviceStationEmailLabel.Text = product.ApplicationForm.ServiceStation.User1.FirstOrDefault().UserEmail;

            // time information
            applyLabel.Text = product.ApplicationForm.ApplicationDateTime.Value.ToString("yyyy-MM-dd HH:mm");
            receiveLabel.Text = product.ApplicationForm.ReceiveDateTime.Value.ToString("yyyy-MM-dd HH:mm");
            distributeLabel.Text = product.ApplicationForm.DistributeDateTime.Value.ToString("yyyy-MM-dd HH:mm");
            quotationLabel.Text = product.QuotationDateTime.HasValue ? product.QuotationDateTime.Value.ToString("yyyy-MM-dd HH:mm") : "无报价";
            confirmQuotationLabel.Text = product.ConfirmQuotationDateTime.HasValue ? product.ConfirmQuotationDateTime.Value.ToString("yyyy-MM-dd HH:mm") : "未确认";
            repairCompleteLabel.Text = product.RepairCompleteDateTime.HasValue ? product.RepairCompleteDateTime.Value.ToString("yyyy-MM-dd HH:mm") : "未完成";
            if (product.ApplicationForm.Gathering != null)
            {
                confirmGathingLabel.Text = product.ApplicationForm.Gathering.PayDateTime.HasValue ? product.ApplicationForm.Gathering.PayDateTime.Value.ToString("yyyy-MM-dd HH:mm") : "未付款";
                sendTicketLabel.Text = product.ApplicationForm.Gathering.SendTicketDate.HasValue ? product.ApplicationForm.Gathering.SendTicketDate.Value.ToString("yyyy-MM-dd HH:mm") : "未发货";
            }
            clientReceiveLabel.Text = product.ClientReceiveDateTime.HasValue ? product.ClientReceiveDateTime.Value.ToString("yyyy-MM-dd HH:mm") : "未收货";
            completeLabel.Text = product.ApplicationForm.CompleteDateTime.HasValue ? product.ApplicationForm.CompleteDateTime.Value.ToString("yyyy-MM-dd HH:mm") : "未完成";

            if (!product.SparePartApplyForm.IsLoaded)
            {
                product.SparePartApplyForm.Load();
            }
            if (product.SparePartApplyForm.Count > 0)
            {
                SparePartApplyForm spaForm = product.SparePartApplyForm.FirstOrDefault();
                sparePartApplyLabel.Text = spaForm.ApplyTime.Value.ToString("yyyy-MM-dd HH:mm");
                sparePartDealWithLabel.Text = spaForm.DealWithTime.HasValue ? spaForm.DealWithTime.Value.ToString("yyyy-MM-dd HH:mm") : "0000-00-00";
                sparePartSendLabel.Text = spaForm.SendTime.HasValue ? spaForm.SendTime.Value.ToString("yyyy-MM-dd HH:mm") : "0000-00-00";
                sparePartReceiveLabel.Text = spaForm.ReceiveTime.HasValue ? spaForm.ReceiveTime.Value.ToString("yyyy-MM-dd HH:mm") : "0000-00-00";
                sparePartReturnLabel.Text = spaForm.SendReturnTime.HasValue ? spaForm.SendReturnTime.Value.ToString("yyyy-MM-dd HH:mm") : "0000-00-00";
                sparePartReceiveReturnLabel.Text = spaForm.ReceiveReturnTime.HasValue ? spaForm.ReceiveReturnTime.Value.ToString("yyyy-MM-dd HH:mm") : "0000-00-00";
            }
            if (!product.UpgradeForm.IsLoaded)
            {
                product.UpgradeForm.Load();
            }
            if (product.UpgradeForm.Count > 0)
            {
                UpgradeForm upform = product.UpgradeForm.FirstOrDefault();
                upgradeApplyLabel.Text = upform.ApplyTime.Value.ToString("yyyy-MM-dd");
                upgradeDealWithLabel.Text = upform.DealWithTime.HasValue ? upform.DealWithTime.Value.ToString("yyyy-MM-dd") : "0000-00-00";
                upgradeSendLabel.Text = upform.SendTime.HasValue ? upform.SendTime.Value.ToString("yyyy-MM-dd") : "0000-00-00";
                upgradeReceiveLabel.Text = upform.ReceiveTime.HasValue ? upform.ReceiveTime.Value.ToString("yyyy-MM-dd") : "0000-00-00";
            }

            if (!product.ReturnToFactoryForm.IsLoaded)
            {
                product.ReturnToFactoryForm.Load();
            }
            if (product.ReturnToFactoryForm.Count > 0)
            {
                ReturnToFactoryForm rtfform = product.ReturnToFactoryForm.FirstOrDefault();
                returnToFacTimeLabel.Text = rtfform.ApplyDate.Value.ToString("yyyy-MM-dd");
                returnToFacReceiveLabel.Text = rtfform.ReceiveDate.HasValue ? rtfform.ReceiveDate.Value.ToString("yyyy-MM-dd") : "0000-00-00";
            }

            //original application
            ApplicationProductService appServie = new ApplicationProductService();
            ApplicationProductHistory appHistory = appServie.GetProductHistoryByID(product.ApplicationProductID);
            productTypeLabel.Text = appHistory.ProductType.ProductTypeName;
            productSerialNumberLabel.Text = appHistory.ProductSerialNumber;
            problemTypeLabel.Text = appHistory.ProblemType.Name;
            problemDescriptionLabel.Text = appHistory.ProductProblemDescription;
            productAppearenceLabel.Text = appHistory.ProductAppearance;
            buyDateLabel.Text = appHistory.ProductPurchaseDateTime.Value.ToString("yyyy-MM-dd");
            repairTypeLabel.Text = appHistory.ProductRepairType;
            if (!product.QuatationForm.IsLoaded)
            {
                product.QuatationForm.Load();
            }
            double cost = 0;
            string reason = string.Empty;
            string accessory = string.Empty;
            string costString = string.Empty;
            string quotationString = string.Empty;
            foreach (QuatationForm quotationForm in product.QuatationForm)
            {
                cost += quotationForm.Cost;
                reason += "," + quotationForm.Reason;
                accessory += "," + quotationForm.AccessoryName;
                costString += "," + quotationForm.Cost.ToString();
                quotationString += "," + quotationForm.Reason;
            }
            quatationLabel.Text = cost.ToString();
            if (!string.IsNullOrEmpty(reason))
            {
                reasonLabel.Text = reason.Substring(1);
            }
            if (product.IsDecideToRepair.HasValue)
            {
                if (product.IsDecideToRepair == true)
                {
                    isDecideToRepairLabel.Text = "是";
                }
                else
                {
                    isDecideToRepairLabel.Text = "否";
                }
            }

            // maintain information
            maintainProductTypeLabel.Text = product.ProductType.ProductTypeName;
            repairAccessoryLabel.Text = product.RepairAccessory;
            appearanceLabel.Text = product.ProductAppearance;
            reasonOfProblemLabel.Text = product.ReasonToProblem;
            repairPlaceLabel.Text = product.AccessoryPlace;
            changeTypeLabel.Text = product.ChangeType;
            replaceSerialNumberLabel.Text = product.ReplaceProductSerialNumber;
            if (!product.ProblemAnalyzeReference.IsLoaded)
            {
                product.ProblemAnalyzeReference.Load();
            }
            problemAnalyzeLabel.Text = GetProblemAnalyze(product.ProblemAnalyze);
            //resultLabel.Text = product.ProductProblemDescription;
            resultLabel.Text = product.BJXXRemark;
            if (!product.UserReference.IsLoaded)
            {
                product.UserReference.Load();
            }
            engineerLabel.Text = product.User.UserName;

            //gather information
            gatheringProductTypeLabel.Text = product.ProductType.ProductTypeName;
            gatheringProductSerialLabel.Text = product.ProductSerialNumber;
            if (!string.IsNullOrEmpty(accessory))
            {
                gatheringaccessoryLabel.Text = accessory.Substring(1);
            }
            if (!string.IsNullOrEmpty(costString))
            {
                gatheringcostLabel.Text = costString.Substring(1);
            }
            if (!string.IsNullOrEmpty(quotationString))
            {
                gatheringReasonLabel.Text = quotationString.Substring(1);
            }
            totalCostLabel.Text = cost.ToString();

            // requisition information
            if (!product.RequisitionFormReference.IsLoaded)
            {
                product.RequisitionFormReference.Load();
            }
            Label14.Text = product.SendWay;
            if (product.RequisitionForm != null)
            {
                if (!product.RequisitionForm.LogisticCompanyReference.IsLoaded)
                {
                    product.RequisitionForm.LogisticCompanyReference.Load();
                }
                Label13.Text = product.RequisitionForm.DistributionFormID;
                Label15.Text = product.RequisitionForm.WayOfTransport;
                Label16.Text = product.RequisitionForm.LogisticCompany.Name;
                Label17.Text = product.RequisitionForm.RequisitionPerson;
            }
            fhProductTypeLabel.Text = product.ProductType.ProductTypeName;
            fhproductSerialLabel.Text = product.ProductSerialNumber;
            fhRemarkLabel.Text = product.Remark;

            //client comment
            if (!product.ApplicationFormReference.IsLoaded)
            {
                product.ApplicationFormReference.Load();
            }
            if (!product.ApplicationForm.ConfirmReceiveProduct.IsLoaded)
            {
                product.ApplicationForm.ConfirmReceiveProduct.Load();
            }
            clientCommentConfirmGatheringLabel.Text = product.ClientReceiveDateTime.HasValue ? "是" : "否";
            if (product.ApplicationForm.ConfirmReceiveProduct.FirstOrDefault() != null)
            {
                clientCommentSuggestingLabel.Text = product.ApplicationForm.ConfirmReceiveProduct.FirstOrDefault().Comment;
                clientCommentConfirmWayLabel.Text = product.ApplicationForm.ConfirmReceiveProduct.FirstOrDefault().ConfirmWay;
            }
            //maintainQualityLabel.Text = 
        }

        private string GetProblemAnalyze(ProblemAnalyze problemAnalyze)
        {
            string result = string.Empty;
            if (problemAnalyze != null)
            {
                if (!string.IsNullOrEmpty(problemAnalyze.A))
                {
                    result += "," + problemAnalyze.A;
                }
                if (!string.IsNullOrEmpty(problemAnalyze.B))
                {
                    result += "," + problemAnalyze.B;
                }
                if (!string.IsNullOrEmpty(problemAnalyze.C))
                {
                    result += "," + problemAnalyze.C;
                }
                if (!string.IsNullOrEmpty(problemAnalyze.D))
                {
                    result += "," + problemAnalyze.D;
                }
                if (!string.IsNullOrEmpty(problemAnalyze.E))
                {
                    result += "," + problemAnalyze.E;
                }
            }
            if (!string.IsNullOrEmpty(result))
            {
                result = result.Substring(1);
            }
            return result;
        }
    }
}
